package com;

import com.student.Student;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest(String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }

    public static class MyBatisTest {
        private SqlSessionFactory sqlSessionFactory;
        private SqlSession sqlSession;
        @Before
        public void init() {
            // 定义读取文件名
            String resource = "mybatis-config.xml";
            // 创建流
            Reader reader = null;
            try {
                //读取mybatis-config.xml文件到reader对象中
                reader = Resources.getResourceAsReader(resource);
                //初始化mybatis,创建SqlSessionFactory类的对象
                SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
                // 创建sqlSession对象
                sqlSession=sqlMapper.openSession();
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
        @Test
        public void findAllStudentTest(){
            List<Student>list=
                    sqlSession.selectList("com.mapper.StudentMapper.findAllStudent");
            for(Student student:list){
                System.out.println(student);
            }
        }
        @After
        public void destory(){
            sqlSession.commit();
            sqlSession.close();

        }
    }
}
